﻿using CCWin;
using SuperMap.Data;
using SuperMap.Mapping;
using SuperMap.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace MDF
{
    /// <summary>
    /// 选择要素
    /// </summary>
    public partial class SJPPXZYS : CCSkinMain
    {
        //变量
        private Map[] m_Maps;

        private DataInfo m_Datainfo;

        /// <summary>
        /// 静态选择要素窗体控制
        /// </summary>
        public static SJPPXZYS m_xZYS;

        /// <summary>
        /// 选择要素窗口初始化
        /// </summary>
        public SJPPXZYS(Map[] Maps)
        {
            InitializeComponent();

            m_Maps = Maps;
            m_xZYS = this;
            m_Datainfo = new DataInfo();

            FullMaps();

        }
        /// <summary>
        /// 填充控件
        /// </summary>
        private void FullMaps()
        {
            try
            {
                //填充地图名称，来激活对应控件
                foreach (Map map in m_Maps)
                {
                    this.Maps_Combo.Items.Add(map.Name);
                }

                //初始数量0
                this.SelectsCount_Label.Text = "已选择要素个数：0";
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 选择地图时，给对应Map Control提供选择功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Maps_Combo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                //添加对应选择功能
                MDF_Main.mDF_Main.Map_Control1_Select(this.Maps_Combo.SelectedIndex);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 清空选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Clear_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //清空选择
                MDF_Main.mDF_Main.Map_Control1_Select(3);
                this.SelectsCount_Label.Text = "已选择要素个数：0";
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 确定生成选择集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OK_Button_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.Name_textBox.Text == "")
                {
                    MessageBox.Show("选择集名称为空！", "警告：");
                }
                else
                {
                    if (this.SelectsCount_Label.Text != "已选择要素个数：0")
                    {
                        //获取信息
                        Map map = m_Maps[this.Maps_Combo.SelectedIndex];
                        string selectionsName = this.Name_textBox.Text;
                        //选择集信息保存
                        if (m_Datainfo.SaveSelectionsInfo(map, selectionsName))
                        {
                            //重新填充父窗体下拉框
                            SJPP.m_sjpp.FillSets();
                            //关闭窗体，并清空选择
                            this.Close();
                        }
                        
                    }
                    else
                    {
                        MessageBox.Show("已选择要素数量为零", "提示：");
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 限制输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Name_textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                Regex reg = new Regex("^\\w$");
                if (this.Name_textBox.Text == "" && e.KeyChar >= '0' && e.KeyChar <= '9')
                {
                    e.Handled = true;
                }
                else if (!reg.IsMatch(e.KeyChar.ToString()) && e.KeyChar != '\b')
                {
                    e.Handled = true;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 加载时，隐藏父窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XZYS_Load(object sender, EventArgs e)
        {
            try
            {
                SJPP.m_sjpp.Visible = false;
            }
            catch (Exception)
            {

                throw;
            }     
        }
        /// <summary>
        /// 关闭时，打开父窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XZYS_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                SJPP.m_sjpp.Visible = true;
                //清空选择
                MDF_Main.mDF_Main.Map_Control1_Select(3);
                //还原图层右键效果
                MDF_Main.mDF_Main.Map_Control1_Select(-1);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
    }
}
